<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
        <div class="logo-container">
          <img src="../assets/logo.png" alt="Logo" v-show="!isCollapse">
          <span v-show="!isCollapse">管理后台</span>
        </div>

        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          router>

          <el-menu-item index="/admin/dashboard">
            <i class="el-icon-s-home"></i>
            <span slot="title">首页</span>
          </el-menu-item>

          <el-submenu index="pet">
            <template slot="title">
              <i class="el-icon-sunset"></i>
              <span>宠物管理</span>
            </template>
            <el-menu-item index="/admin/pet-manage">宠物信息</el-menu-item>
            <el-menu-item index="/admin/pet-care-manage">宠物护理</el-menu-item>
            <el-menu-item index="/admin/health-manage">健康检测</el-menu-item>
            <el-menu-item index="/admin/sale-pet-manage">待售宠物</el-menu-item>
          </el-submenu>

          <el-menu-item index="/admin/staff-manage">
            <i class="el-icon-user"></i>
            <span slot="title">员工管理</span>
          </el-menu-item>

          <el-submenu index="service">
            <template slot="title">
              <i class="el-icon-s-tools"></i>
              <span>服务管理</span>
            </template>
            <el-menu-item index="/admin/service-manage">服务项目</el-menu-item>
            <el-menu-item index="/admin/foster-manage">宠物寄养</el-menu-item>
          </el-submenu>

          <el-menu-item index="/admin/order-manage">
            <i class="el-icon-shopping-bag-2"></i>
            <span slot="title">订单管理</span>
          </el-menu-item>

          <el-submenu index="system">
            <template slot="title">
              <i class="el-icon-setting"></i>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/admin/system-setting">系统设置</el-menu-item>
            <el-menu-item index="/admin/auth-manage">权限管理</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
               class="collapse-icon"
               @click="toggleCollapse">
            </i>
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item v-for="item in breadcrumbs" :key="item.path">
                {{ item.name }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>

          <div class="header-right">
            <el-badge :value="notificationCount" class="notification-badge">
              <i class="el-icon-bell"></i>
            </el-badge>
            <el-dropdown @command="handleCommand" class="user-dropdown">
              <span class="user-info">
                <el-avatar :size="32">{{ username }}</el-avatar>
                <span style="margin-left: 10px">{{ username }}</span>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="password">修改密码</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主要内容 -->
        <el-main class="main-content">
          <router-view></router-view>
        </el-main>

        <!-- 底部 -->
        <el-footer class="footer">
          <div>© 2024 宠物管理系统. All Rights Reserved.</div>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { logout } from '@/api/auth'

export default {
  name: 'AdminLayout',
  data () {
    return {
      isCollapse: false,
      username: '管理员',
      notificationCount: 5,
      breadcrumbs: []
    }
  },
  computed: {
    activeMenu () {
      return this.$route.path
    }
  },
  watch: {
    '$route' () {
      this.updateBreadcrumbs()
    }
  },
  created () {
    // 从localStorage获取用户信息
    const userInfo = localStorage.getItem('userInfo')
    if (userInfo) {
      try {
        const user = JSON.parse(userInfo)
        this.username = user.realName || user.username || '管理员'
      } catch (e) {
        this.username = localStorage.getItem('username') || '管理员'
      }
    } else {
      this.username = localStorage.getItem('username') || '管理员'
    }
    this.updateBreadcrumbs()
  },
  methods: {
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    updateBreadcrumbs () {
      const route = this.$route
      const breadcrumbMap = {
        '/admin/dashboard': '首页',
        '/admin/pet-manage': '宠物信息管理',
        '/admin/pet-care-manage': '宠物护理管理',
        '/admin/health-manage': '健康检测管理',
        '/admin/sale-pet-manage': '待售宠物管理',
        '/admin/staff-manage': '员工管理',
        '/admin/service-manage': '服务管理',
        '/admin/foster-manage': '寄养管理',
        '/admin/order-manage': '订单管理',
        '/admin/system-setting': '系统设置',
        '/admin/auth-manage': '权限管理'
      }

      if (route.path !== '/admin/dashboard' && breadcrumbMap[route.path]) {
        this.breadcrumbs = [{ name: breadcrumbMap[route.path], path: route.path }]
      } else {
        this.breadcrumbs = []
      }
    },
    handleCommand (command) {
      switch (command) {
        case 'profile':
          this.$message.info('个人资料')
          break
        case 'password':
          this.$message.info('修改密码')
          break
        case 'logout':
          this.handleLogout()
          break
      }
    },
    handleLogout () {
      this.$confirm('确认退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用退出登录API
        logout().then(() => {
          // 清除本地存储
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          this.$message.success('已退出登录')
          this.$router.push('/login')
        }).catch(() => {
          // 即使API调用失败，也清除本地token并跳转
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          this.$router.push('/login')
        })
      })
    }
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.el-container {
  height: 100%;
}

.sidebar {
  background-color: #304156;
  transition: width 0.3s;
}

.logo-container {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: white;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #404852;
}

.logo-container img {
  height: 32px;
}

.sidebar-menu {
  border-right: none;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 200px;
}

.header {
  background: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.collapse-icon {
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.collapse-icon:hover {
  color: #409EFF;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 25px;
}

.notification-badge {
  cursor: pointer;
  font-size: 20px;
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
}

.main-content {
  background-color: #f0f2f5;
  overflow-y: auto;
}

.footer {
  background: white;
  text-align: center;
  color: #999;
  border-top: 1px solid #e8e8e8;
  height: 48px !important;
  line-height: 48px;
  padding: 0;
}
</style>
